package com.jthinker.web.controller.admin;

import com.jthinker.bean.vo.QueryResult;
import com.jthinker.bean.vo.UserInfo;
import com.jthinker.service.UserService;
import com.jthinker.web.controller.common.BaseController;
import com.jthinker.web.controller.front.LoginController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chen.gang on 15-2-23.
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController
{
    @Resource
    private UserService userService;

    @RequestMapping
    public String view()
    {
        return "user/userList";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Object list(HttpServletRequest request)
    {
        QueryResult<Map<String, Object>> queryResult = userService.query(getSearchFilter(request));
        return getSuccessMessage(queryResult);
    }

    @RequestMapping("/newUser")
    public String newUser()
    {
        return "user/userView";
    }

    @RequestMapping("/userAdd")
    public String addUser(HttpServletRequest request)
    {
        List<String> errors = new ArrayList<String>();

        String username = request.getParameter("username");
        if (StringUtils.isBlank(username))
        {
            errors.add("用户名不能为空!");
        }
        else if (!userService.isUnique(username))
        {
            errors.add("用户名[" + username + "]已存在!");
        }

        String password = request.getParameter("password");
        String rePassword = request.getParameter("repassword");

        if (StringUtils.isBlank(password))
        {
            errors.add("密码不能为空!");
        }
        else if (!password.equals(rePassword))
        {
            errors.add("两次密码不一致!");
        }

        String nickname = request.getParameter("nickname");

        if (StringUtils.isBlank(nickname))
        {
            errors.add("昵称不能为空!");
        }

        if (!errors.isEmpty())
        {
            request.setAttribute("errors", errors);
            return "user/userView";
        }

        Map<String, Object> record = new HashMap<String, Object>();
        record.put("username", username);
        record.put("password", password);
        record.put("nickname", nickname);

        userService.createUser(record);

        return "redirect:/admin/user";
    }

    @RequestMapping("/pass")
    public String pass()
    {
        return "user/userPass";
    }

    @RequestMapping("/updatePass")
    public String updatePass(String oldpass, String password, String repassword, Model model, HttpServletRequest request)
    {
        List<String> errors = new ArrayList<String>();

        if (StringUtils.isBlank(oldpass) || StringUtils.isBlank(password) || StringUtils.isBlank(repassword))
        {
            errors.add("密码不能为空!");
        }

        if (!StringUtils.equals(password, repassword))
        {
            errors.add("新密码不一致!");
        }

        UserInfo current = getCurrentUser(request);
        String username = current.getUsername();
        String oldSaltPass = LoginController.getSaltPassword(oldpass, username);
        UserInfo userInfo = userService.login(username, oldSaltPass);

        if (userInfo == null)
        {
            errors.add("原密码不正确!");
        }

        if (!errors.isEmpty())
        {
            model.addAttribute("errors", errors);
            return "user/userPass";
        }

        String newSaltPass = LoginController.getSaltPassword(password, username);
        userService.updatePass(newSaltPass, current.getId());

        return "redirect:/admin/index";
    }
}
